package com.company.project.Scheduled;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.company.project.email.MailService;
import com.company.project.model.*;
import com.company.project.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Component
public class ScheduledController {
    @Resource
    private KrWokrdayDivsSumsService krWokrdayDivsSumsService;
    @Resource
    private KrWokrdaySumsService krWokrdaySumsService;
    @Resource
    private KrWokrdayDetailService krWokrdayDetailService;
    @Resource
    private EiccrequirementwarningLyService eiccrequirementwarningLyService;
    @Resource
    private EiccrequirementwarningLySqService eiccrequirementwarningLySqService;
    @Autowired
    private MailService mailService;

//    @Scheduled(cron = "0 0 0 1/1 * ? ")
//    @Scheduled(cron = "0 */3 * * * ?")
    public void listAll() throws FileNotFoundException {
        System.out.println("定时任务启动");
        test1();
        String[] arr = {"wang.ping@ofilm.com", "Yang.Liu22@ofilm.com", "xu.zhenhui@ofilm.com", "zhang.lina@ofilm.com", "Ajiao.Leng@o-film.com", "Xiaoli.Jiang@ofilm.com",
                "Huolan.Wu@ofilm.com", "Sisi.Wan2@ofilm.com", "huafen.wang@o-film.com", "jiang.aihua@ofilm.com", "Xitian.Dai@ofilm.com",
                "jywy@ofilm.com", "huang.fan@ofilm.com", "hongyan.gao@ofilm.com", "fangqin@ofilm.com", "Xiaofeng.Luo2@ofilm.com", "Tian.Lu@ofilm.com",
                "Yang.Liu22@ofilm.com", "panrong@ofilm.com", "huangrong@ofilm.com", "lijuan.wang@ofilm.com"};
//        String[] arr ={"Yang.Liu22@ofilm.com"};
        String[] co = {};
        mailService.sendAttachmentsMail(arr, co, "6休1管控", "做六休一管控统计，请查收", "C:\\Users\\liuyang\\78.xlsx");
        System.out.println("定时任务结束");
    }

    @Scheduled(cron = "0 */3 * * * ?")
//    @Scheduled(cron = "0 15 10 ? * *")
    public void Eicc() throws FileNotFoundException {
        System.out.println("定时任务Eicc启动");
        test2();
        String[] arr = {"N144859@o-film.com", "tan.maolin@o-film.com","xu.li@ofilm.com","xiao.minjie@o-film.com", "hu.yihui@o-film.com", "zhao.yuanhui@o-film.com", "Jiansheng.Zhou@o-film.com", " nie.shibo@o-film.com", "lu.feng@o-film.com", "mao.junzhi@o-film.com", "ji.hailong@o-film.com", "wu.qingyin@o-film.com", " hu.haigen@o-film.com", "zhang.bo@o-film.com", "Danhong.Chen@o-film.com", "Li.Zhang2@o-film.com", "wangxin@o-film.com", " cxh@o-film.com", "liu.hao03@o-film.com", "huang.fan@o-film.com", " peng.haitao@o-film.com", "chen.xiaohong@o-film.com", "caojun@o-film.com", "Xitian.Dai@o-film.com", " rong.huang2@o-film.com"};
        String[] co = {"Vicky.Zha@o-film.com", "xiao.min@o-film.com", " wei.ping@o-film.com", " Jiayun.xiong@o-film.com", "xie.chongjiao@o-film.com", " Shuisheng.Guo@o-film.com", "fusheng.wang@o-film.com", "wang.maochun@o-film.com", "xiaohua@o-film.com", " wuliangjun@o-film.com", "liuzhaoshun@o-film.com", "hou.xiaowei@o-film.com", "lym@o-film.com", "yuan.chunfeng@o-film.com", " anson.wang@o-film.com", "jiang.feng@o-film.com", "peng.yonghua@o-film.com", "wu.huolin@o-film.com", "li.chuntian@o-film.com", " Huatao.Xia@o-film.com", "Dehua.Yang@o-film.com", "liangbing.wan@o-film.com", "yu.qing@o-film.com", " hu.lu@o-film.com", " hyman.tian@o-film.com", "wu.liaoning@o-film.com", "xdd@o-film.com", "Vicky.Zha@o-film.com", "Sophia.Zeng@o-film.com", " hu.lu@o-film.com", "yangguanjun@o-film.com", "Zhun.Fang@o-film.com", "zhao.ming02@o-film.com", "hans.wang@o-film.com", " ShangLI@o-film.com", "Louis.Chung@o-film.com", "liu.hao03@o-film.com", "Haiqing.Deng@o-film.com", "Wenxiang.Li@o-film.com", " huang.huijun@o-film.com", "Kai.Wang@o-film.com", "zhen.yu@o-film.com", "zeng.xiangsheng@o-film.com"};
        if (eiccrequirementwarningLyService.findAll().size() > 0 || eiccrequirementwarningLySqService.findAll().size() > 0) {
            mailService.sendAttachmentsMail(arr, co, "CSR管控人员周工时预警", "附件为需要执行CSR管控人员周工时预警，请查阅", "D:\\79.xlsx");
        }
        System.out.println("定时任务Eicc结束");
    }

    private void test2() throws FileNotFoundException {
        OutputStream out = new FileOutputStream("D:\\79.xlsx");
        try {
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
            //写第一个sheet, sheet1  数据全是List<String> 无模型映射关系
            Sheet sheet1 = new Sheet(1, 1, EiccrequirementwarningLy.class, "神奇MM", null);
            writer.write(eiccrequirementwarningLyService.findAll(), sheet1);
            Sheet sheet2 = new Sheet(2, 1, EiccrequirementwarningLySq.class, "神奇非MM", null);
            writer.write(eiccrequirementwarningLySqService.findAll(), sheet2);
            writer.finish();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void test1() throws FileNotFoundException {
        List<KrWokrdaySums> krWokrdaySums = new ArrayList<>();
        List<KrWokrdayDivsSums> krWokrdayDivsSums = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (KrWokrdaySums wokrdaySums : krWokrdaySumsService.findAll()) {

            if (simpleDateFormat.format(wokrdaySums.getWorkday()).equals(simpleDateFormat.format(new Date()))) {
                krWokrdaySums.add(wokrdaySums);
            }
        }
        for (KrWokrdayDivsSums divsSums : krWokrdayDivsSumsService.findAll()) {
            if (simpleDateFormat.format(divsSums.getWorkday()).equals(simpleDateFormat.format(new Date()))) {
                krWokrdayDivsSums.add(divsSums);
            }
        }
        OutputStream out = new FileOutputStream("C:\\Users\\liuyang\\78.xlsx");
        try {
            ExcelWriter writer = new ExcelWriter(out, ExcelTypeEnum.XLSX);
            Sheet sheet1 = new Sheet(1, 2, KrWokrdaySums.class, "事业群汇总", null);
            writer.write(krWokrdaySums, sheet1);
            Sheet sheet2 = new Sheet(2, 2, KrWokrdayDivsSums.class, "地区—事业群汇总", null);
            writer.write(krWokrdayDivsSums, sheet2);
            Sheet sheet3 = new Sheet(3, 1, KrWokrdayDetail.class, "人员明细", null);
            writer.write(krWokrdayDetailService.findAll(), sheet3);
            writer.finish();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
